package com.springdoor.web.controller.goods;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.springdoor.common.core.domain.entity.SysMenu;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springdoor.common.annotation.Log;
import com.springdoor.common.core.controller.BaseController;
import com.springdoor.common.core.domain.AjaxResult;
import com.springdoor.common.enums.BusinessType;
import com.springdoor.common.core.domain.entity.SprdGoodsAttr;
import com.springdoor.goods.service.ISprdGoodsAttrService;
import com.springdoor.common.utils.poi.ExcelUtil;

/**
 * 商品属性Controller
 *
 * @author llshs
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/goods/attr")
public class SprdGoodsAttrController extends BaseController
{
    @Autowired
    private ISprdGoodsAttrService attrService;

    /**
     * 查询商品属性列表
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:list')")
    @GetMapping("/list")
    public AjaxResult list(SprdGoodsAttr sprdGoodsAttr)
    {
        List<SprdGoodsAttr> list = attrService.selectSprdGoodsAttrList(sprdGoodsAttr);
        return success(list);
    }

    /**
     * 导出商品属性列表
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:export')")
    @Log(title = "商品属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SprdGoodsAttr sprdGoodsAttr)
    {
        List<SprdGoodsAttr> list = attrService.selectSprdGoodsAttrList(sprdGoodsAttr);
        ExcelUtil<SprdGoodsAttr> util = new ExcelUtil<SprdGoodsAttr>(SprdGoodsAttr.class);
        util.exportExcel(response, list, "商品属性数据");
    }

    /**
     * 获取商品属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:query')")
    @GetMapping(value = "/{attrId}")
    public AjaxResult getInfo(@PathVariable("attrId") Long attrId)
    {
        return success(attrService.selectSprdGoodsAttrByAttrId(attrId));
    }

    /**
     * 新增商品属性
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:add')")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SprdGoodsAttr goodsAttr)
    {
        if (!attrService.checkGoodsAttrCodeUnique(goodsAttr))
        {
            return error("新增属性失败，属性编码 " + goodsAttr.getAttrCode() + " 已存在");
        }
        if (!attrService.checkGoodsAttrNameUnique(goodsAttr))
        {
            return error("新增属性失败，属性名称 " + goodsAttr.getAttrName() + " 已存在");
        }
        goodsAttr.setCreateBy(getUsername());
        goodsAttr.setUpdateBy(getUsername());
        return toAjax(attrService.insertSprdGoodsAttr(goodsAttr));
    }

    /**
     * 修改商品属性
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:edit')")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SprdGoodsAttr goodsAttr)
    {
        if (!attrService.checkGoodsAttrCodeUnique(goodsAttr))
        {
            return error("修改属性失败，属性编码 " + goodsAttr.getAttrCode() + " 已存在");
        }
        if (!attrService.checkGoodsAttrNameUnique(goodsAttr))
        {
            return error("修改属性失败，属性名称 " + goodsAttr.getAttrName() + " 已存在");
        }
        goodsAttr.setUpdateBy(getUsername());
        return toAjax(attrService.updateSprdGoodsAttr(goodsAttr));
    }

    /**
     * 删除商品属性
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:remove')")
    @Log(title = "商品属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attrIds}")
    public AjaxResult remove(@PathVariable Long[] attrIds)
    {
        return toAjax(attrService.deleteSprdGoodsAttrByAttrIds(attrIds));
    }

    /**
     * 获取商品属性选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        return success(attrService.selectGoodsAttrAll());
    }

    /**
     * 获取商品属性选择框列表
     */
    @GetMapping("/optionMenuselect")
    public AjaxResult optionMenuselect() {
        return success(attrService.selectGoodsAttrMenu());
    }

    @GetMapping("/treeSelect")
    public AjaxResult treeSelect() {
        List<SprdGoodsAttr> list = attrService.selectValidGoodsAttrAll();
        return success(attrService.buildAttrTreeSelect(list));
    }

    /**
     * 根据 类型id 获取属性树
     *
     * @param typeId
     * @return
     */
    @GetMapping(value = "/typeAttrTreeOptions/{typeId}")
    public AjaxResult typeAttrTreeOptions(@PathVariable("typeId") Long typeId) {
        List<SprdGoodsAttr> list = attrService.selectTypeAttrListByTypeId(typeId);
        return success(attrService.buildAttrTreeSelect(list));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/typeAttrTreeSelect/{typeId}")
    public AjaxResult typeAttrTreeSelect(@PathVariable("typeId") Long typeId)
    {
        List<SprdGoodsAttr> list = attrService.selectValidGoodsAttrAll();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", attrService.selectAttrListByTypeId(typeId));
        ajax.put("attrs", attrService.buildAttrTreeSelect(list));
        return ajax;
    }


    /**
     * 获取未配置的属性
     */
    @PreAuthorize("@ss.hasPermi('goods:config:edit')")
    @PostMapping("findAttrsWithoutConfig")
    public AjaxResult findAttrsWithoutConfig(@RequestBody Map<String, Object> param) {
        if (param == null || param.size() < 2 || (param.get("gNumber") == null && param.get("typeId") == null)) {
            return error("参数错误");
        }
        try {
            List<Long> arr = (List<Long>) param.get("attrIds");
            if (arr == null || arr.isEmpty()) {
                param.put("attrIds", null);
            }
        } catch (Exception e) {
            return error("参数错误");
        }
        return success(attrService.findAttrsWithoutConfig(param));
    }
}
